package com.hkyl.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.hkyl.hospital.entity.Patient;
import com.hkyl.hospital.entity.PatientExample;
import com.hkyl.hospital.mapper.custom.CustomPatientMapper;
import com.hkyl.hospital.service.PatientService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



@Service
@Transactional
public class PatientServiceImpl implements PatientService {

    @Autowired
    private CustomPatientMapper patientMapper;

    @Override
    public int insert(Patient patient) {
        return patientMapper.insert(patient);
    }

    @Override
    public int delete(Integer patientId ,Integer status) {
        if(status == 1){
            return patientMapper.fakeDelete(patientId);
        }else{
            return  patientMapper.changeStatus(patientId);
        }

    }

    @Override
    public int update(Patient patient) {
        return patientMapper.updateByPrimaryKeySelective(patient);
    }

    @Override
    public List<Patient> select(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Patient> patients = patientMapper.selectByExample(null);
        return patients;
    }

    @Override
    public List<Patient> search(String patientName, String patientCard, Integer pageNumber, Integer pageSize) {
        PatientExample patientExample = new PatientExample();
        if(patientName == null){
            patientExample.or().andPatientCardLike("%" + patientCard + "%");
        } else if (patientCard == null) {
            patientExample.or().andPatientNameLike("%" + patientName + "%");
        }else

        patientExample.or().andPatientNameLike("%" + patientName + "%").andPatientCardLike("%" + patientCard + "%");
        PageHelper.startPage(pageNumber, pageSize);
        List<Patient> patients = patientMapper.selectByExample(patientExample);
        return patients;
    }

    @SneakyThrows
    @Override
    public Patient login(Patient patient) {
        PatientExample patientExample = new PatientExample();
        patientExample.or().andUsernameEqualTo(patient.getUsername()).andPasswordEqualTo(patient.getPassword());
        List<Patient> patients = patientMapper.selectByExample(patientExample);
        if (patients.size() == 0) {
            throw new RuntimeException("您输入的账号或密码不对");
        }
        return patients.get(0);
    }
    @SneakyThrows
    @Override
    public Patient regisger(Patient patient) {
        PatientExample patientExample = new PatientExample();
        patientExample.or().andUsernameEqualTo(patient.getUsername());
        List<Patient> patients = patientMapper.selectByExample(patientExample);
        if (patients.size() > 0) {
            throw new RuntimeException("您输入的账号已经存在");
        }
        patientMapper.insertSelective(patient);
        return patient;
    }

    @Override
    public int resetpwd(Integer patientId) {
        return patientMapper.resetpwd(patientId);
    }
}
